{% load static %}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>添加用户</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="{% static '/layui/css/layui.css'%}">
</head>
<body>
<div class="layui-layout layui-layout-admin">
  {% include 'header.html' %}
  <div class="layui-body">
    <div style="padding: 15px;">
      <h2>添加用户</h2>
      <form class="layui-form" lay-filter="userForm">
        <div class="layui-form-item">
          <label class="layui-form-label">姓名</label>
          <div class="layui-input-block">
            <input type="text" name="username" lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label">年龄</label>
          <div class="layui-input-block">
            <input type="number" name="age" lay-verify="required|number" placeholder="请输入年龄" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label">分类</label>
          <div class="layui-input-block">
            <select name="category" lay-verify="required">
              <option value="">请选择分类</option>
              <option value="1">学生</option>
              <option value="2">老师</option>
              <option value="3">工人</option>
            </select>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">部门</label>
          <div class="layui-input-block">
            <select name="department" lay-verify="required">
              <option value="">请选择部门</option>
              <option value="1">人事部</option>
              <option value="2">技术部</option>
              <option value="3">市场部</option>
              <option value="4">财务部</option>
              <option value="5">行政部</option>
            </select>
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label">头像</label>
          <div class="layui-input-block">
            <button type="button" class="layui-btn" id="uploadBtn">
              <i class="layui-icon layui-icon-upload"></i> 图片上传
            </button>
            <div style="width: 132px;">
              <div class="layui-upload-list">
                <img class="layui-upload-img" id="preview" style="width: 100%; height: 92px;">
                <div id="uploadText"></div>
              </div>
              <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="uploadProgress">
                <div class="layui-progress-bar" lay-percent=""></div>
              </div>
            </div>
          </div>
        </div>

        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="userSubmit">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="history.back()">返回</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>

<script src="{% static '/layui/layui.js' %}"></script>
<script>
layui.use(['form', 'upload', 'element'], function(){
  var form = layui.form;
  var upload = layui.upload;
  var element = layui.element;
  var $ = layui.$;

  // 确保表单渲染
  form.render();

  // 图片上传
  upload.render({
    elem: '#uploadBtn',
    url: '', // 上传接口
    before: function(obj){
      obj.preview(function(index, file, result){
        $('#preview').attr('src', result);
      });
      element.progress('uploadProgress', '0%');
    },
    done: function(res){
      if(res.code > 0){
        return layer.msg('上传失败');
      }
      $('#uploadText').html('');
    },
    error: function(){
      var uploadText = $('#uploadText');
      uploadText.html('<span style="color: #FF5722;">上传失败</span>');
    },
    progress: function(n){
      element.progress('uploadProgress', n + '%');
    }
  });

  // 表单提交
  form.on('submit(userSubmit)', function(data){
    var formData = data.field;

    // 组装用户数据
    var user = {
      id: Date.now(),
      username: formData.username,
      age: formData.age,
      category: formData.category,
      department: formData.department,
      icon: $('#preview').attr('src') || "default.png"
    };

    // Ajax提交
    $.ajax({
      url: './doadd',
      method: 'POST',
      data: user,
      success: function(res) {
        layer.msg('提交成功');
        // 可以在这里处理成功后的跳转
      },
      error: function(err) {
        layer.msg('提交失败');
      }
    });

    return false; // 阻止表单跳转
  });
});
</script>
</body>
</html>